//瓦片网络，瓦片网络类型
//用法：构造瓦片网络，瓦片X,Y位置转中心点坐标，任意坐标转瓦片X,Y位置

var tileNetType = {
	Rect_Horizontal : 0,
	Rect_Vertical : 1,
	Triangle_Horizontal : 2,
	Triangle_Vertical : 3,
	Hexagon_Horizontal : 4,
	Hexagon_Vertical : 5
};

var TileNet = function(netType, tileWidth, tileHeight, xOffset, yOffset)
{
	this._netType = netType;
	this._tileWidth = tileWidth;
	this._tileHeight = tileHeight;
	this._xOffset = xOffset;
	this._yOffset = yOffset;
	
	this.TileToCoord = function(tile){
		switch (this._netType)
		{
		case 0:
			return {};
		case 1:
			return {};
		case 2:
			return {};
		case 3:
			return {};
		case 4:
			return {
				x: this._xOffset + this._tileWidth * 0.75 * tile.x,
				y: this._yOffset + this._tileHeight * ( tile.x%2==0 ? tile.y : 0.5+tile.y)
			};
		case 5:
			return {
				x: this._xOffset + this._tileWidth * ( tile.y%2==0 ? tile.x : 0.5+tile.x),
				y: this._yOffset + this._tileHeight * 0.75 * tile.y
			};
		}
	};
	
	this.CoordToTile = function(coord){
		switch (this._netType)
		{
		case 0:
			return {};
		case 1:
			return {};
		case 2:
			return {};
		case 3:
			return {};
		case 4:
			return {};
		case 5:
			var yn = coord.y - this._yOffset + 0.5 * this._tileHeight;
			var xn = coord.x - this._xOffset;
			var y1 = yn % (0.75 * this._tileHeight);
			var yid = Math.floor(yn / (0.75 * this._tileHeight));
			var dual = yid%2==0;
			if (y1 >= 0.25 * this._tileHeight){
				return {
					x: Math.floor(( dual ? xn + 0.5 * this._tileWidth : xn ) / this._tileWidth),
					y: yid
				};
			}else{
				var x1 = xn % this._tileWidth;
				var xid = Math.floor(xn / this._tileWidth);
				var identifier = 2 * y1 * this._tileWidth;
				if (dual){
					if (identifier > (this._tileHeight * x1)){
						return { x:xid, y:yid };
					}else if (identifier > (this._tileHeight * (this._tileWidth - x1))){
						return { x:xid+1, y:yid };
					}else{
						return { x:xid, y:yid-1 };
					}
				}else{
					if (identifier < (this._tileHeight * (0.5 * this._tileWidth - x1))){
						return { x:xid, y:yid-1 };
					}else if (identifier < (this._tileHeight * (x1 - 0.5 * this._tileWidth))){
						return { x:xid+1, y:yid-1 };
					}else{
						return { x:xid, y:yid };
					}
				}
			}
		}
	};
	
	this.NeighborTileXYs = function(tile)
	{
		var moves = this.NeighborTileMoves(tile);
		var XYs = [];
		for (var idx in moves){
			XYs[idx] = {x:tile.x+moves[idx].x, y:tile.y+moves[idx].y};
		}
		return XYs;
	};
	
	//竖六边形的六个邻接多边形的顺序分别如下，相当于笛卡尔坐标系旋转
	//var neighbour = { 0：左侧，1：左下，2：右下，3：右侧，4：右上,5，左上 }
	this.NeighborTileMoves = function(tile){
		switch (this._netType)
		{
		case 0:
			return [];
		case 1:
			return [];
		case 2:
			return [];
		case 3:
			return [];
		case 4:
			return [];
		case 5:
			if (tile.y%2==0)
				return [ {x:-1, y:0 },
				         {x:-1, y:-1},
				         {x:0,  y:-1},
				         {x:1,  y:0 },
				         {x:0,  y:1 },
				         {x:-1, y:1 }
				       ];
			else
				return [ {x:-1, y:0 },
				         {x:0,  y:-1},
				         {x:1,  y:-1},
				         {x:1,  y:0 },
				         {x:1,  y:1 },
				         {x:0,  y:1 }
				         ];
		}
	};

	this.HeadingToNeighbor = function(heading){
		switch (this._netType)
		{
		case 0:
			return -1;
		case 1:
			return -1;
		case 2:
			return -1;
		case 3:
			return -1;
		case 4:
			return -1;
		case 5:
			var heading_tmp = heading - 150;
			while (heading_tmp <= 0)
				heading_tmp += 360;
			return Math.floor(heading_tmp / 60);
		}
	};
	
	//Clockwise is +, anticlockwise is -
	this.TwoTileHeading = function(tilebase, tileheading){
		switch (this._netType)
		{
		case 0:
			return -1;
		case 1:
			return -1;
		case 2:
			return -1;
		case 3:
			return -1;
		case 4:
			return -1;
		case 5:
			var coordbase = this.TileToCoord(tilebase);
			var coordheading = this.TileToCoord(tileheading);
			if (coordbase.x == coordheading.x){
				return coordheading.y > coordbase.y ? 90 : -90;
			}else{
				return Math.atan2(coordheading.y - coordbase.y, coordheading.x - coordbase.x) / Math.PI * 180;
			}
				
		}
	};
};

